// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  let db = cloud.database()
  const wxContext = cloud.getWXContext()
  let evaluate = {}
  let jud = 0
  let openid = wxContext.OPENID
  let image = event.image
  let name = event.name
  db.collection("evaluate").get()
    .then(res => {
      evaluate = res.data
      for (let i = 0; i < evaluate.length; i++) {
        jud = 0
        for (let j = 0; j < evaluate[i].evaluate.length; j++) {
          if (evaluate[i].evaluate[j].openid === openid) {
            evaluate[i].evaluate[j].image = image
            evaluate[i].evaluate[j].name = name
            jud = 1
          }
        }
        if (jud === 1) {
          db.collection('evaluate').doc(evaluate[i]._id).update({
              data: {
                evaluate: evaluate[i].evaluate
              }
            })
            .then(e => {
              console.log('更新成功', e)
            })
            .catch(err => {
              console.log("更新失败", err)
            })
        }
      }
    })
}